package com.fw.service.persistence.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;

import com.fw.service.persistence.MerchantProfilePersistenceService;
import com.fw.service.persistence.dao.MerchantProfileDAO;
import com.fw.service.persistence.dto.MerchantProfile;

public class MerchantProfilePersistenceServiceImpl implements MerchantProfilePersistenceService {

    @Autowired
    private MerchantProfileDAO merchantProfileDAO;

    @Override
    public List<MerchantProfile> selectByMerchantIdAndStatus(final String merchantId, final String status) {

	Specification<MerchantProfile> specification = new Specification<MerchantProfile>() {
	    @Override
	    public Predicate toPredicate(Root<MerchantProfile> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
		List<Predicate> predicates = new ArrayList<Predicate>();
		if (merchantId != null && !merchantId.isEmpty()) {
		    predicates.add(cb.like(root.<String> get("merchantId"), merchantId));
		}
		if (status != null && !status.isEmpty()) {
		    predicates.add(cb.equal(root.<String> get("merchantStatus"), status));
		}
		Predicate result = cb.conjunction();
		for (Predicate predicate : predicates) {
		    result.getExpressions().add(predicate);
		}
		return result;
	    }
	};
	return merchantProfileDAO.findAll(specification);
    }

    @Override
    public List<MerchantProfile> selectByStatus(final String status) {
	Specification<MerchantProfile> specification = new Specification<MerchantProfile>() {
	    @Override
	    public Predicate toPredicate(Root<MerchantProfile> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
		List<Predicate> predicates = new ArrayList<Predicate>();
		if (status != null && !status.isEmpty()) {
		    predicates.add(cb.equal(root.<String> get("merchantStatus"), status));
		}
		Predicate result = cb.conjunction();
		for (Predicate predicate : predicates) {
		    result.getExpressions().add(predicate);
		}
		return result;
	    }
	};
	return merchantProfileDAO.findAll(specification);
    }

    @Override
    public MerchantProfile save(MerchantProfile merchantProfile) {
	return merchantProfile == null ? null : merchantProfileDAO.save(merchantProfile);
    }

    @Override
    public MerchantProfile findOne(Long id) {
	return id == null ? null : merchantProfileDAO.findOne(id);
    }

    @Override
    public Page<MerchantProfile> findAll(Specification<MerchantProfile> specification, PageRequest pageRequest) {
	return specification == null || pageRequest == null ? null : merchantProfileDAO.findAll(specification, pageRequest);
    }

    @Override
    public List<Object> selectAllMerchantProfileId() {
	return merchantProfileDAO.selectDistinctMerchantId();
    }

    @Override
    public List<Object> selectAllCompanyNameEn() {
	return merchantProfileDAO.selectDistinctCompanyNameEn();
    }

    @Override
    public List<Object> selectAllCompanyNameTh() {
	return merchantProfileDAO.selectDistinctCompanyNameTh();
    }

    @Override
    public List<Object> selectAllURL() {
	return merchantProfileDAO.selectDistinctURL();
    }

    @Override
    public List<Object> selectAllContact1Name() {
	return merchantProfileDAO.selectDistinctContact1Name();
    }

    @Override
    public List<Object> selectAllContact2Name() {
	return merchantProfileDAO.selectDistinctContact2Name();
    }

    @Override
    public List<Object> selectAllContact3Name() {
	return merchantProfileDAO.selectDistinctContact3Name();
    }

    @Override
    public List<Object> selectAllContact4Name() {
	return merchantProfileDAO.selectDistinctContact4Name();
    }

    @Override
    public List<MerchantProfile> selectByNotStatus(final String status) {
	Specification<MerchantProfile> specification = new Specification<MerchantProfile>() {
	    @Override
	    public Predicate toPredicate(Root<MerchantProfile> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
		List<Predicate> predicates = new ArrayList<Predicate>();
		if (status != null && !status.isEmpty()) {
		    predicates.add(cb.notEqual(root.<String> get("merchantStatus"), status));
		}
		Predicate result = cb.conjunction();
		for (Predicate predicate : predicates) {
		    result.getExpressions().add(predicate);
		}
		return result;
	    }
	};
	return merchantProfileDAO.findAll(specification, new Sort(Direction.ASC, "merchantId"));
    }

}
